ChemAxon Synergy integration workshop | 您所在的位置:网站首页 › slf4j actual binding › ChemAxon Synergy integration workshop |
ChemAxon Synergy integration workshop
Table of Contents 1. Prerequisites 2. Generate a Spring Boot app 2.1 Create a Spring Boot application 2.2 Try to open health check page 3. Add a welcome page 3.1 Open the generated Maven project in your favourite IDE. 3.2 Add the HTML page 3.3 Run/restart the application 3.4 Check welcome page in a browser 4. App info endpoint 4.1 Create class AppInfoController 4.2 Run/restart the application 4.3 Check application info in a browser 5. Deploy to Heroku 5.1 Create a Git repo and commit your sources 5.2 Create Heroku app and deploy the application 6. Register app into Synergy 7. Implement authentication 7.1 Add security dependencies 7.2 Enable Oauth2 SSO for Spring Boot app 7.3 Set configuration required by OAuth2 7.4 Commit & Deploy 7.5 Open your app directly on Heroku 8. Print user info 8.1 Extract custom token data 8.2 Add the user info to the welcome page 8.3 Commit & Deploy 9. Implement authentication of rest endpoints 9.1 Create resource server configuration 9.2 Modify SynergySecurityConfiguration 9.3 Commit & Deploy 10. Logout 10.1 Logout initiated from your application 10.2 Logout initiated from Synergy 10.3 Commit & Deploy 11. Demonstrate application to application communication 11.1 Create ServiceController and implement a producer service 11.2 Create helper class for obtaining token for your application 11.3 Create RestTemplateConfiguration 11.4 Create DiscoveryClient 11.5 Create App2AppCommunication 11.6 Commit & Deploy 11.7 Open your app directly on Heroku, ask for name of another service (or use yours) 11.8 Display application logs to check communication 1. PrerequisitesJava Development Kit 8 Your favourite Java IDE Spring Boot CLI: https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-installing-spring-boot.html#getting-started-installing-the-cli Git client Heroku account: https://signup.heroku.com/ Heroku CLI: https://devcenter.heroku.com/articles/heroku-cli CXN Pass account: https://pass.chemaxon.com/ {info} Optionally check these to verify settings: java -version java version "1.8.x" Java(TM) SE Runtime Environment (build 1.8.x) Java HotSpot(TM) 64-Bit Server VM ... spring --version Spring CLI v1.5.3.RELEASE git --version git version 2.9.0. heroku --version heroku-cli/5.9.1-3d5ebd1 ... go1.7.5 2. Generate a Spring Boot app 2.1 Create a Spring Boot application spring init --package-name=com.example --dependencies=web,actuator --boot-version=1.5.6.RELEASE example-synergy-app cd example-synergy-app ./mvnw spring-boot:run 2.2 Try to open health check pageOpen http://localhost:8080/health 3. Add a welcome page 3.1 Open the generated Maven project in your favourite IDE. 3.2 Add the HTML pageThe easiest way to create a welcome page is to add an index.html to src/main/resources/static/: Example Synergy app Hello Synergy! 3.3 Run/restart the application ./mvnw spring-boot:run 3.4 Check welcome page in a browserOpen http://localhost:8080/ 4. App info endpointOur application鈥檚 app-info endpoint should look like this: { "displayName": "Example Synergy app", "address": "http://localhost:8080/", "identities": [ { "category": "service", "type": "computation" }, { "category": "application", "type": "service" } ], "features": [ { "namespace": "synergy/health", "attributes": { "url": "http://localhost:8080/health" } }, { "namespace": "synergy/icon", "attributes": { "url": "https://www.gravatar.com/avatar/457372368cbaf7fce1427ce46fc4b199?s=64&d=identicon" } }, { "namespace": "synergy/logout", "attributes": { "url": "https://morning-brook-95472.herokuapp.com/front-channel-logout" } }, { "namespace": "producer-service", "attributes": { "url": "http://localhost:8080/produce" } } ] }We define the following features here: The health endpoint that Synergy uses to check if your application is running fine The icon that is used in Synergy for your application The logout endpoint that Synergy calls when a user logs out from Synergy (we will implement this later) A service called producer-service , that is a sample for a custom service provided by your application (we will implement this later) To learn more about these features, check the Synergy Feature Catalogue. 4.1 Create class AppInfoControllerThe class should be placed in the com.example package so that Spring Boot finds it automatically. package com.example; import java.security.MessageDigest; import java.util.Arrays; import java.util.LinkedHashMap; import java.util.Map; import java.util.Optional; import java.util.function.Supplier; import javax.servlet.http.HttpServletRequest; import javax.xml.bind.DatatypeConverter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.support.ServletUriComponentsBuilder; import org.springframework.web.util.UriComponentsBuilder; @Controller public class AppInfoController { private static final Logger LOG = LoggerFactory.getLogger(AppInfoController.class); @Value("${producer.name:#{null}}") private String producerName; @RequestMapping("/app-info") @ResponseBody Object appInfo(final HttpServletRequest request) throws Exception { Supplier uriBuilder = () -> ServletUriComponentsBuilder.fromContextPath(request) ; // TODO: give the application some nice name String applicationDisplayName = "Example Synergy app"; Map info = new LinkedHashMap(); info.put("displayName", applicationDisplayName); String address = uriBuilder.get().replacePath("/").toUriString(); info.put("address", address); info.put("identities", Arrays.asList( identity("service", "computation"), identity("application", "service"))); // generate a unique hash for the application String hash = DatatypeConverter.printHexBinary( MessageDigest.getInstance("md5").digest(address.getBytes())).toLowerCase(); // TODO: give the producer service some custom unique name String producerServiceName = Optional.ofNullable(producerName).orElse(hash + "-producer"); info.put("features", Arrays.asList( feature("synergy/health", uriBuilder.get().replacePath("/health").toUriString()), feature("synergy/icon", String.format("https://www.gravatar.com/avatar/%s?s=64&d=identicon", hash)), feature("synergy/logout", uriBuilder.get().replacePath("/front-channel-logout").toUriString()), feature(producerServiceName, uriBuilder.get().replacePath("/produce").toUriString()))); return info; } /** Helper methods for assembling app-info json **/ private static Map identity(final String category, final String type) { Map identity = new LinkedHashMap(); identity.put("category", category); identity.put("type", type); return identity; } private static Map feature(final String namespace, final String url) { Map feature = new LinkedHashMap(); feature.put("namespace", namespace); Map attributes = new LinkedHashMap(); attributes.put("url", url); feature.put("attributes", attributes); return feature; } } 4.2 Run/restart the application ./mvnw spring-boot:run 4.3 Check application info in a browserOpen http://localhost:8080/app-info 5. Deploy to Heroku 5.1 Create a Git repo and commit your sources git init git add . git commit -m "first commit" 5.2 Create Heroku app and deploy the application heroku login heroku create git push heroku master heroku open 6. Register app into SynergyIf you send us your deployment鈥檚 application info URL, we鈥檒l register it for you. 7. Implement authentication 7.1 Add security dependenciesAdd new dependencies in your pom.xml : org.springframework.boot spring-boot-starter-security org.springframework.security.oauth spring-security-oauth2 org.springframework.security spring-security-jwt 7.2 Enable Oauth2 SSO for Spring Boot appIt can be done by adding @EnableOAuth2Sso annotation. {primary} Security must be disabled on health and app-info endpoints. Create class SynergySecurityConfiguration, extend WebSecurityConfigurerAdapter and override configure method: package com.example; import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration @EnableOAuth2Sso public class SynergySecurityConfiguration extends WebSecurityConfigurerAdapter { @Override public void configure(final WebSecurity web) throws Exception { web.ignoring().antMatchers( "/app-info", "/health"); } } 7.3 Set configuration required by OAuth2Rename empty config file application.properties to application.yml . Add these to application.yml : synergy: url: https://team1.synergy-dev.cxcloud.io security: oauth2: client: clientId: [your app's client id] clientSecret: [your app's client secret] accessTokenUri: ${synergy.url}/oauth/token userAuthorizationUri: ${synergy.url}/oauth/authorize resource: jwt.key-uri: ${synergy.url}/public/publickey/ssh-rsa.json{info} Ask for your client id and client secret! 7.4 Commit & Deploy git add . git commit -m "configure authentication" git push heroku master 7.5 Open your app directly on Heroku heroku openThere should be a redirect to authenticate you when opening the application. (You might not notice it, when already logged in to Synergy.) 8. Print user infoThe logged in user's info is contained in the JWT access token provided by Synergy. To demonstrate how it can be accessed, we will print it on the welcome page. 8.1 Extract custom token dataCreate class SynergyAccessTokenConfigurer and implement JwtAccessTokenConverterConfigurer : package com.example; import java.util.Collection; import java.util.Collections; import java.util.Map; import org.springframework.boot.autoconfigure.security.oauth2.resource.JwtAccessTokenConverterConfigurer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter; import org.springframework.security.oauth2.provider.token.DefaultUserAuthenticationConverter; import org.springframework.security.oauth2.provider.token.UserAuthenticationConverter; import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; @Configuration public class SynergyAccessTokenConfigurer implements JwtAccessTokenConverterConfigurer { @Bean UserAuthenticationConverter userAuthenticationConverter() { return new DefaultUserAuthenticationConverter() { @Override public Authentication extractAuthentication(final Map token) { Collection |
CopyRight 2018-2019 实验室设备网 版权所有 |